package com.example.guavaSrudy.basicJoiner;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class GuavaBasicUse {

    public static void main(String[] args) {
//        测试效果
//        String delimiter = "|";
//        List<String> strings = Lists.newArrayList("a", "b", "c");
//        System.out.println("原始的toString"+strings.toString());
//        String result = buidString(strings,delimiter);
//        System.out.println("这是一般操作："+result);
//        String result2= buidStringUseGuava(strings,delimiter);
//        System.out.println("这是guava操作："+result2);
//        GuavaJoinerTest();
        testMapJoiner();
    }
//    定义一长串带定位符字符串 一般操作方法。。有点复杂
    public static String buidString(List<String> stringList, String delimiter) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : stringList) {
            if (s != null) {
                stringBuilder.append(s).append(delimiter);
            }
        }
//        移除最后一个分割符
        stringBuilder.setLength(stringBuilder.length()-delimiter.length());
        return stringBuilder.toString();
    }
// 使用guara来操作
    public static String buidStringUseGuava(List<String> stringList, String delimiter) {
//        就一条代码解决。 如果你想遇到控制使用指定值替换,使用useForNull
//        Joiner.on(delimiter).useForNull("no value").join(stringList);

        return Joiner.on(delimiter).skipNulls().join(stringList);
    }
    public static void GuavaJoinerTest() {
//        StringBuilder buider = new StringBuilder();
//        Joiner stringJoiner = Joiner.on("|").skipNulls();
//        //the useForNull() method returns a new instance of the Joiner!
////        stringJoiner.useForNull("missing");
//        StringBuilder s = stringJoiner.appendTo(buider,"foo","bar","baz");
//        System.out.println(stringJoiner.join(null,"bar"));
        try ( FileWriter fileWriter = new FileWriter(new File("path.txt"))){
            List<Date> dateList = getDates();
            fileWriter.append("aaaaaaa+++aaaaa");
            Joiner joiner = Joiner.on("#").useForNull(" ");
            //returns the FileWriter instance with the values appended into it
            joiner.appendTo(fileWriter,dateList);
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static List<Date> getDates() {

        Calendar instance = Calendar.getInstance();
        instance.set(2018 + 1900, 12, 21);
        Date time = instance.getTime();
        List dates = Lists.newArrayList();
        dates.add(time);
        instance.set(2019 , 12, 21);
        dates.add(instance.getTime());
        return dates;
    }

    /**
     * 创建linkmap使用字符串类型的key,value.
     * 然后Joiner把键值对连接成一个字符串用#分割，键和值用=分割
     *
     */
    public static void testMapJoiner() {
        //Using LinkedHashMap so that the original
//        order is preserved
        String expectedString = "Washington D.C=Redskins#New York City=Giants#Philadelphia=Eagles#Dallas=Cowboys";
        Map<String,String> testMap = Maps.newLinkedHashMap();
        testMap.put("Washington D.C","Redskins");
        testMap.put("New York City","Giants");
        testMap.put("Philadelphia","Eagles");
        testMap.put("Dallas","Cowboys");
        String returnedString = Joiner.on("#").
                withKeyValueSeparator("=").join(testMap);
        System.out.println(returnedString);
    }
}
